package cate.common.table.item;

import cate.common.table.ESheet;
import cate.common.util.random.RWList;
import easy.java.dev.note.NoteClass;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "碎片合成")
public class HeroPartCompositeSheet extends ESheet<HeroPartCompositeRow> {

    @Override
    public String getSheetName() {
        return "碎片合成";
    }

    private final Map<Integer, RWList<HeroPartCompositeRow>> groupMap = new HashMap<>();

    @Override
    protected void onRowCreated(HeroPartCompositeRow row) {
        RWList<HeroPartCompositeRow> rwl = groupMap.get(row.groupId);
        if (rwl == null) {
            rwl = new RWList<>();
            groupMap.put(row.groupId, rwl);
        }
        rwl.add(row);
    }

    @Override
    protected void onLoaded() {
        groupMap.values().forEach(RWList::setup);
    }

    /**
     * 根据星组随机英雄ID
     */
    public HeroPartCompositeRow randomHero(int groupId) {
        RWList<HeroPartCompositeRow> rwl = groupMap.get(groupId);
        if (rwl == null) {
            return null;
        }
        return rwl.get();
    }

    public HeroPartCompositeRow chooseHero(int groupId, int tid) {
        RWList<HeroPartCompositeRow> rwl = groupMap.get(groupId);
        if (rwl == null || rwl.list == null || rwl.list.isEmpty()) {
            return null;
        }

        return rwl.list.stream()
                .filter(row -> row.heroTid == tid)
                .findFirst()
                .orElse(null);
    }
}
